package com.topscomm.main.config;

import com.rabbitmq.client.Channel;
import com.topscomm.mis.mq.MisBasicHandler;
import com.topscomm.pub.util.SpringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author liwanpeng
 * @version 1.0
 * @description: TODO
 * @date 2021/4/29 9:50
 */
@Configuration
public class RabbitMqConfig {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Bean
    public SimpleMessageListenerContainer messageContainer(ConnectionFactory connectionFactory,MisBasicHandler misBasicHandler) {
//        ConnectionFactory connectionFactory = (ConnectionFactory) SpringUtil.getBean("connectionFactory");

        //加载处理消息A的队列
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        //设置接收多个队列里面的消息，这里设置接收队列A
        //假如想一个消费者处理多个队列里面的信息可以如下设置：
        //container.setQueues(queueA(),queueB(),queueC());
        container.setQueues(misQueue());
        container.setExposeListenerChannel(true);
        //设置最大的并发的消费者数量
        container.setMaxConcurrentConsumers(10);
        //最小的并发消费者的数量
        container.setConcurrentConsumers(1);
        //设置确认模式手工确认
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        container.setMessageListener(misBasicHandler);
        return container;
    }
    @Bean
    public Queue misQueue() {
        return new Queue("topscomm.mpm.misDeptHeaderData.sync", true); //队列持久
    }
    @Bean
    public MisBasicHandler misBasicHandler() {
        return new MisBasicHandler();
    }
}

